package com.weetop.akly.activity.mine;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.weetop.akly.R;
import com.weetop.akly.activity.meessage.ShenpiListActivity;
import com.weetop.akly.activity.meessage.SignDayRecordActivity;
import com.weetop.akly.base.BaseActivity;
import com.weetop.akly.base.mvp.BaseModel;
import com.weetop.akly.base.mvp.BaseObserver;
import com.weetop.akly.base.mvp.BasePresenter;
import com.weetop.akly.bean.ApproveItem;
import com.weetop.akly.bean.BaseCountBean;
import com.weetop.akly.bean.MemberBean;
import com.weetop.akly.bean.MsgListBean;
import com.weetop.akly.presenter.MembersHolidaysRecordPresenter;
import com.weetop.akly.presenter.WorkApplyPresent;
import com.weetop.akly.utils.BaseUtils;
import com.weetop.akly.view.MembersHolidaysRecordView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 创建日期：2021/11/23  09:38
 * 类说明:
 *
 * @author：86152
 */
public class MembersHolidaysRecordActivity extends BaseActivity<MembersHolidaysRecordPresenter> implements MembersHolidaysRecordView {

    @BindView(R.id.recycler)
    RecyclerView recyclerView;

    @BindView(R.id.srl_data)
    SmartRefreshLayout srData;
    @BindView(R.id.tv_holiday_sup)
    TextView tv_holiday_sup;
    @BindView(R.id.tv_type)
    TextView typeTextView;
    private CommonRecyclerAdapter<ApproveItem> adapter;
    private ArrayList<ApproveItem> data = new ArrayList<>();

    private Map<String, Object> params = new HashMap<>();

    private int page = 1;

    private int pageSize = 10;

    private int leave_type = 1;

    private ArrayList<String> typeList = new ArrayList<>();

    private QMUIBottomSheet chooseGroupSheet;

    private String askLeaveType;

    @Override
    protected MembersHolidaysRecordPresenter createPresenter() {
        return new MembersHolidaysRecordPresenter(this, this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_members_holidays_record);
        ButterKnife.bind(this);
        setTitleInfo("员工假期使用记录");
        initMsgList();
        initQjlx();
        refresh();
    }


    private void initQjlx() {
        typeList.clear();
        typeList.add("年假");
        typeList.add("事假");
        typeList.add("病假");
        typeList.add("婚假");
        typeList.add("产假");
        typeList.add("陪产假");
        typeList.add("工伤假");

        askLeaveType = typeList.get(0);
        leave_type = 1;
        typeTextView.setText(askLeaveType);
        //初始化考勤组弹窗
        QMUIBottomSheet.BottomListSheetBuilder chooseHeadImgBottomSheetBuilder = new QMUIBottomSheet.BottomListSheetBuilder(this);
        for (String datum : typeList) {
            chooseHeadImgBottomSheetBuilder.addItem(datum);
        }
        chooseHeadImgBottomSheetBuilder.setOnSheetItemClickListener((dialog, itemView, position, tag) -> {
            askLeaveType = typeList.get(position);
            leave_type = position + 1;
            typeTextView.setText(askLeaveType);
            refresh();
            dialog.dismiss();
        });
        chooseGroupSheet = chooseHeadImgBottomSheetBuilder.build();
        typeTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chooseGroupSheet.show();
            }
        });
    }


    private void refresh() {
        adapter.clear();
        page = 1;
        fetchRecord();
    }


    private void loadNextPage() {
        page++;
        fetchRecord();
    }

    private void fetchRecord() {
        params.put("leave_type", leave_type);
        params.put("page", page);
        params.put("per", pageSize);
        addDisposable(apiServer.vacationLogList(params), new BaseObserver<BaseModel<BaseCountBean<ApproveItem>>>(this) {
            @Override
            public void onSuccess(BaseModel<BaseCountBean<ApproveItem>> o) {
                tv_holiday_sup.setText("当前" + askLeaveType + "余额：" + o.getData().getSurplus() + "天");
                adapter.addAll(o.getData().getList_data());
                srData.finishLoadMore();
                srData.finishRefresh();
            }

            @Override
            public void onError(String msg) {
                tv_holiday_sup.setText("当前" + askLeaveType + "余额：" + 0 + "天");
                srData.finishLoadMore();
                srData.finishRefresh();
            }
        });
    }

    protected void initMsgList() {
        recyclerView.setItemAnimator(null);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new CommonRecyclerAdapter<ApproveItem>(mActivity, R.layout.item_members_holidays_record) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, ApproveItem item, int position) {
                helper.setText(R.id.tv_time, item.getCreate_time());
                helper.setText(R.id.tv_days, "-" + item.getDk_day());
                String startTime = TimeUtils.date2String(TimeUtils.string2Date(item.getStart_time()), new SimpleDateFormat("yyyy-MM-dd"));
                String endTIme = TimeUtils.date2String(TimeUtils.string2Date(item.getEnd_time()), new SimpleDateFormat("yyyy-MM-dd"));
                helper.setText(R.id.tv_start_end_time, startTime + " 至 " + endTIme);
                helper.setText(R.id.tv_desc, item.getLeave_reason());
                if (item.getStatus() == 1) {
                    //通过
                    helper.setText(R.id.tv_status, "审批通过");
                }
                if (item.getStatus() == 0) {
                    helper.setText(R.id.tv_status, "待审核");
                    //待审核
                }
                if (item.getStatus() == -1) {
                    helper.setText(R.id.tv_status, "拒绝");
                    //拒绝
                }
            }
        };
        recyclerView.setAdapter(adapter);
        srData.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                loadNextPage();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refresh();
            }
        });
    }
}
